<?php
/**
 * Rather than adding all theme functions into a single file, this functions file is used for 
 * initializing the theme framework, which activates files in the order that it needs. Users
 * should create a child theme and make changes to its functions.php file (not this one).
 *
 * @package dkret3
 */

// Check for required WordPress version
global $wp_version;
if ( version_compare( $wp_version, '3.0', '<' ) ) :
    update_option( 'template', 'default' );
    update_option( 'stylesheet', 'default' );
    delete_option( 'current_theme' );
    $theme = get_current_theme();
    do_action('switch_theme', $theme);
    wp_die( 'Dkret3 requires at least WordPress 3.0', 'Theme Error!');
endif;

/**
 * Constants provide convenient aliases for filesystem paths. Dkret's many
 * files live in a number of directories, so keeping includes simple is greatly
 * helped by a sane set of path constants. 
 * 
 */
define( 'THEME_DIR', get_template_directory() );
define( 'THEME_URI', get_template_directory_uri() );
define( 'CHILD_THEME_DIR', get_stylesheet_directory() );
define( 'CHILD_THEME_URI', get_stylesheet_directory_uri() );

define( 'THEME_LIBRARY', THEME_DIR . '/library' );
define( 'THEME_ADMIN', THEME_LIBRARY . '/admin' );
define( 'THEME_FUNCTIONS', THEME_LIBRARY . '/functions' );
define( 'THEME_LANGUAGES', THEME_LIBRARY . '/languages' );

define( 'THEME_IMAGES', THEME_URI . '/library/images' );
define( 'THEME_JS', THEME_URI . '/library/js' );
define( 'THEME_LAYOUTS', THEME_URI . '/library/layouts' );
define( 'THEME_STYLES', THEME_URI . '/library/css' );

/**
 * Core function files.
 * 
 * These files will be loaded whenever WordPress is. They include the core
 * functions that dkret3 requires to run properly, functions that are used
 * as custom template tags which are added to the various dkret3 action hooks,
 * classes of dkret specific widgets and all shortcode functions.
 * 
 */
require_once( THEME_FUNCTIONS . '/core.php' );
require_once( THEME_FUNCTIONS . '/custom-template-tags.php' );
require_once( THEME_FUNCTIONS . '/widgets.php' );
require_once( THEME_FUNCTIONS . '/shortcodes.php' );

/**
 * Set the content width based on the theme's design and stylesheet.
 *
 * Used to set the width of images and content. Should be equal to the width the theme
 * is designed for, generally via the style.css stylesheet.
 */
if ( ! isset( $content_width ) )
	$content_width = 640;

/**
 * Initialize the basic functions of the dkret3 theme framework.
 *
 * @adds dkret_setup_options() to 'after_setup_theme' Hook To set up all options
 * @adds dkret_delete_options() to the 'switch_theme' Hook To delete all dkret3 options if a user switches themes
 * @adds dkret_add_menu() to 'admin_menu' Hook For adding the dkret3 settings page
 * @adds dkret_update_options() to the WordPress options handler Hook
 * @adds dkret_register_sidebars() to the 'widgits_init' Hook To register widgetized areas
 * @adds dkret_register_widgets() to the 'widgits_init' Hook To unregiser WordPress Widgets and register all dkret3 widgets
 * @adds dkret_enqueue_scripts() to the 'wp_enque_scripts' Hook To inject needed js and css files to the page <head>
 * @adds dkret_add_body_class() to the 'body_class' Hook To add browser classes to the page <body>
 * @adds dkret_add_menu_class() to the 'wp_page_menu' Hook To add the .sf-menu class to the wp_page_menu() function
 * @removes rsd_link(), wlwmanifest_link() and wp_generator() functions from the blogs <head>
 * @adds do_shortcode() to the 'widget_text' Hook To allow the use of shortcodes inside Widgets
 * @adds wptexturize(), convert_smilies(), convert_chars() and wpautop() functions to the 'archive_meta' Hook
 * 
 * @since 3.6 
 */
add_action( 'after_setup_theme', 'dkret_setup_options' );
add_action( 'switch_theme', 'dkret_delete_options' );

add_action( 'admin_menu', 'dkret_add_menu' );
add_action( 'admin_post_dkret_update_options', 'dkret_update_options' );

add_action( 'widgets_init', 'dkret_register_sidebars' );
add_action( 'widgets_init', 'dkret_register_widgets' );

add_action( 'wp_enqueue_scripts', 'dkret_enque_scripts' );

add_action( 'wp_print_styles', 'dkret_disable_styles' );

add_filter( 'body_class', 'dkret_add_body_classes' );
add_filter( 'wp_page_menu', 'dkret_add_menu_class' );

if ( get_dkret_option( 'remove_meta_information' ) ) :
    remove_action( 'wp_head', 'rsd_link' );
    remove_action( 'wp_head', 'wlwmanifest_link' );
    remove_action( 'wp_head', 'wp_generator' );
endif;

add_filter( 'widget_text', 'do_shortcode' );

add_filter( 'archive_meta', 'wptexturize' );
add_filter( 'archive_meta', 'convert_smilies' );
add_filter( 'archive_meta', 'convert_chars' );
add_filter( 'archive_meta', 'wpautop' );

/**
 * Add all custom-template-tags to various WordPress and dkret3 action hooks.
 *
 * @since 3.6
 */

// add the site title
add_action( 'dkret_header', 'dkret_site_title' );
add_action( 'dkret_header', 'dkret_site_description' );

// build the #access menu
add_action( 'dkret_after_header', 'dkret_add_access_menu' );
add_action( 'dkret_after_access_menu', 'dkret_rss_link' );
add_action( 'dkret_after_access_menu', 'dkret_gallery_link' );

// build the header for every entry
add_action( 'dkret_before_entry', 'dkret_entry_title' ); // add the title
add_action( 'dkret_before_entry', 'dkret_entry_meta_before' ); // add meta information before posts - author, date and edit link

// add AdSense blocks
add_action( 'dkret_after_header', 'dkret_leaderboard' );
add_action( 'dkret_before_entry', 'dkret_adsense_before' ); // add Google AdSense to posts
add_action( 'dkret_after_entry', 'dkret_adsense_after' ); // add Google AdSense to posts bottom

// build the footer for every entry
add_action( 'dkret_after_entry', 'dkret_entry_meta' ); // add meta information

// add navigation links to posts and pages
add_action( 'dkret_after_singular', 'dkret_single_post_navigation' );
add_action( 'dkret_after_content', 'dkret_post_navigation' );

// add comment title and comment meta
add_action( 'dkret_before_comment', 'dkret_comment_meta' );

// add footer text to #footer
add_action( 'dkret_footer', 'dkret_footer_text' );

/**
 * Add shortcodes.
 *
 * @since 3.6
 */

/* Add theme-specific shortcodes. */
add_shortcode( 'separator', 'dkret_separator_shortcode' );
add_shortcode( 'theme-link', 'dkret_theme_link_shortcode' );
add_shortcode( 'child-link', 'dkret_child_theme_link_shortcode' );
add_shortcode( 'wp-link', 'dkret_wp_link_shortcode' );	
add_shortcode( 'site-link', 'dkret_site_link_shortcode' );
add_shortcode( 'login-link', 'dkret_login_link_shortcode' );
add_shortcode( 'the-year', 'dkret_year_shortcode' );
add_shortcode( 'check-html', 'dkret_check_html_shortcode' );
add_shortcode( 'check-css', 'dkret_check_css_shortcode' );
add_shortcode( 'theme-version', 'dkret_theme_version_shortcode' );
add_shortcode( 'child-version', 'dkret_child_theme_version_shortcode' );
add_shortcode( 'query-count', 'dkret_query_counter_shortcode' );

/* Add entry-specific shortcodes. */
add_shortcode( 'entry-title', 'dkret_entry_title_shortcode' );
add_shortcode( 'entry-calendar', 'dkret_entry_calendar_shortcode' );
add_shortcode( 'entry-author', 'dkret_entry_author_shortcode' );
add_shortcode( 'entry-published', 'dkret_entry_published_shortcode' );
add_shortcode( 'entry-terms', 'dkret_entry_terms_shortcode' );
add_shortcode( 'entry-categories', 'dkret_entry_categories_shortcode' );
add_shortcode( 'entry-tags', 'dkret_entry_tags_shortcode' );
add_shortcode( 'entry-comments', 'dkret_entry_comments_shortcode' );
add_shortcode( 'entry-edit-link', 'dkret_entry_edit_link_shortcode' );

/* Add comment-specific shortcodes. */
add_shortcode( 'comment-published', 'dkret_comment_date_link_shortcode' );
add_shortcode( 'comment-author', 'dkret_comment_author_shortcode' );
add_shortcode( 'comment-edit-link', 'dkret_comment_edit_link_shortcode' );
add_shortcode( 'comment-reply-link', 'dkret_comment_reply_link_shortcode' );
add_shortcode( 'comment-permalink', 'dkret_comment_permalink_shortcode' );
//add_shortcode( 'comment-number', 'dkret_comment_number_shortcode' );

/* Add content-specific shortcodes. */
add_shortcode( 'adsense', 'dkret_adsense_shortcode' );
add_shortcode( 'rss', 'dkret_subscribe_rss_shortcode' );
add_shortcode( 'subscribe', 'dkret_subscribe_comments_shortcode' );
add_shortcode( 'twitter', 'dkret_twitter_shortcode' );
add_shortcode( 'donate', 'dkret_donate_shortcode' );

/**
 * Register support for the Wordpress Header Image API.
 * 
 * Some constants need to be set to use the Wordpress Header Image API. Child-Themes
 * can add 'HEADER_TEXTCOLOR', 'HEADER_IMAGE_HEIGHT', and 'HEADER_IMAGE_WIDTH' to
 * their functions.php file to overwrite these defaults. Images uploaded in the Custom
 * Header Panel are automatically cropped to these dimensions.
 *
 */
$layout = get_dkret_option( 'layout_file' );

if ( !defined( 'HEADER_IMAGE') )
    define( 'HEADER_IMAGE', THEME_IMAGES .'/headers/thegreen.jpg' );
    
if ( !defined( 'HEADER_TEXTCOLOR' ) )
    define( 'HEADER_TEXTCOLOR', '048' );
    
if ( !defined( 'HEADER_IMAGE_HEIGHT' ) )
	define( 'HEADER_IMAGE_HEIGHT', apply_filters( 'dkret_header_image_height', 160 ) );

if ( !defined( 'HEADER_IMAGE_WIDTH' ) ) :
    if ( $layout == 'Sidebar-Left-And-Right.css' || $layout == 'Fluidelity.css' )
        define( 'HEADER_IMAGE_WIDTH', apply_filters( 'dkret_header_image_width', 1200 ) );
        
    elseif ( $layout == 'Sidebar-Off.css' )
        define( 'HEADER_IMAGE_WIDTH', apply_filters( 'dkret_header_image_width', 650 ) );
        
    else
        define( 'HEADER_IMAGE_WIDTH', apply_filters( 'dkret_header_image_width', 960 ) );
endif;

add_custom_image_header( 'dkret_custom_header', 'dkret_admin_custom_header' ); // Add the Custom Header Page

/* Default custom headers packaged with the theme. */
register_default_headers( array (
    'thegreen' => array (
        'url' => THEME_IMAGES .'/headers/thegreen.jpg',
        'thumbnail_url' => THEME_IMAGES .'/headers/thegreen-thumbnail.jpg',
        'description' => __( 'The Green', 'dkret' )
    ),
    'tournesol' => array (
        'url' => THEME_IMAGES .'/headers/tournesol.jpg',
        'thumbnail_url' => THEME_IMAGES. '/headers/tournesol-thumbnail.jpg',
        'description' => __( 'Tournesol', 'dkret' )
    ) ) 
);

// We'll be using post thumbnails for custom header images on posts and pages.
// We want them to be same width and height as custom header images (larger images will be auto-cropped to fit).
set_post_thumbnail_size( HEADER_IMAGE_WIDTH, HEADER_IMAGE_HEIGHT, true );

/**
 * At last register support for various WordPress features.
 *
 * @uses load_theme_textdomain() For translation/localization support.
 * @uses add_editor_style() To style the visual editor.
 * @uses add_theme_support() To add support for post thumbnails, navigation menus, and automatic feed links.
 * @uses register_nav_menus() To add the default header menu
 * @uses add_custom_background() To add support for a custom background.
 *
 */
load_theme_textdomain( 'dkret', THEME_LANGUAGES );
add_editor_style();
add_theme_support( 'automatic-feed-links' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'menus' );
register_nav_menus( array('access-menu' => __('Access Menu', 'dkret') ) );
add_custom_background();
?>